<?php
# Part of RAMS unittest fixtures: feed retrieval.
# Copyright (C) 2008 rambla.be
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

class RamsTrafficTest extends UnitTestCase {
    function TestRamsClientlib() {
        $this->UnitTestCase("Rams Traffic Tests");
    }

    function testGetSingleFile() {
      $entriesFound = 0;
      try {
        # check the rootdir
        $rams = new Rams(USER, PWD, BASE);
        # get the feed
        $query = new Rams_TrafficQuery();
        $query->setYear("2009");
        $query->setMonth("all");
        $query->setKind("file");
        # get the feed
        $feed = $rams->getTrafficFeed("/file1.txt", $query);
        foreach ($feed as $entry) {
          switch ($entry->type) {
            case "http":
              $entriesFound++;
              $this->assertEqual($entry->kind, "file");
              $this->assertEqual($entry->content->params->path->text, "/file1.txt");
              $this->assertEqual($entry->content->params->hits->text, HTTP_YEAR_HITS_2009);
              $this->assertEqual($entry->content->params->volume->text, HTTP_YEAR_VOLUME_2009);
              break;
            case "ftp":
              $entriesFound++;
              $this->assertEqual($entry->kind, "file");
              $this->assertEqual($entry->content->params->path->text, "/file1.txt");
              $this->assertEqual($entry->content->params->hits->text, FTP_YEAR_HITS_2009);
              $this->assertEqual($entry->content->params->volume->text, FTP_YEAR_VOLUME_2009);
              break;
            default:
              $this->assertEqual("?", $entry->content->params->name->text);
              break;
          }
        }
      }
      catch(Zend_Gdata_App_Exception $e) {
        print_r($e);
        $this->assertEqual("ok", $e->getResponse()->getBody());
      }
      $this->assertEqual($entriesFound, 2);
    }
    

    function testHttpFilesFromMiscdir() {
      $entriesFound = 0;
      try {
        # check the rootdir
        $rams = new Rams(USER, PWD, BASE);
        # get the feed
        $query = new Rams_TrafficQuery();
        $query->setYear("2009");
        $query->setMonth("all");
        $query->setKind("file");
        $query->setType("http");
        # get the feed
        $feed = $rams->getTrafficFeed("/miscdir", $query);
        foreach ($feed as $entry) {
          // echo "Found " . $entry->type . " entry with kind = " . $entry->kind . "\n";
          // # Retrieve the value of the params child elements
          // echo "Path: " . $entry->content->params->path->text . "\n";
          // echo "Volume: " . $entry->content->params->volume->text . "\n";
          // echo "Hits: " . $entry->content->params->hits->text . "\n";
          // echo "Updated: " . $entry->content->params->updated->text . "\n";
          if (FALSE === strpos($entry->content->params->path->text, "miscfile.avi")) {
              $entriesFound++;
              $this->assertEqual($entry->type, "http");
              $this->assertEqual($entry->content->params->path->text, "/miscdir");
              $this->assertEqual($entry->content->params->hits->text, HTTP_YEAR_HITS_2009);
              $this->assertEqual($entry->content->params->volume->text, HTTP_YEAR_VOLUME_2009);
          }
            else {
              $entriesFound++;
              $this->assertEqual($entry->type, "http");
              $this->assertEqual($entry->content->params->path->text, "/miscdir/miscfile.avi");
              $this->assertEqual($entry->content->params->hits->text, HTTP_YEAR_HITS_2009);
              $this->assertEqual($entry->content->params->volume->text, HTTP_YEAR_VOLUME_2009);
          }
        }
      }
      catch(Zend_Gdata_App_Exception $e) {
        $this->assertEqual("ok", $e->getResponse()->getBody());
      }
      $this->assertEqual($entriesFound, 2);
    }

    function testGetRootForRootDir() {
      $entriesFound = 0;
      try {
        # check the rootdir
        $rams = new Rams(USER, PWD, BASE);
        # get the feed
        $query = new Rams_TrafficQuery();
        $query->setYear("2009");
        $query->setMonth("all");
        $query->setKind("root");
        $query->setType("all");
        # get the feed
        $feed = $rams->getTrafficFeed("/", $query);
        # should contain 3 entries (http, ftp, all)
        foreach ($feed as $entry) {
          switch ($entry->type) {
            case "http":
              $entriesFound++;
              $this->assertEqual($entry->kind, "root");
              $this->assertEqual($entry->content->params->path->text, "/");
              $this->assertEqual($entry->content->params->hits->text, 21 * HTTP_YEAR_HITS_2009);
              $this->assertEqual($entry->content->params->volume->text, 21 * HTTP_YEAR_VOLUME_2009);
              break;
            case "ftp":
              $entriesFound++;
              $this->assertEqual($entry->kind, "root");
              $this->assertEqual($entry->content->params->path->text, "/");
              $this->assertEqual($entry->content->params->hits->text, 21 * FTP_YEAR_HITS_2009);
              $this->assertEqual($entry->content->params->volume->text, 21 * FTP_YEAR_VOLUME_2009);
              break;
            case "all":
              $entriesFound++;
              $this->assertEqual($entry->kind, "root");
              $this->assertEqual($entry->content->params->path->text, "/");
              $this->assertEqual($entry->content->params->hits->text, 21 * ALL_YEAR_HITS_2009);
              $this->assertEqual($entry->content->params->volume->text, 21 * ALL_YEAR_VOLUME_2009);
              break;
            default:
              $this->assertEqual("?", $entry->content->params->name->text);
              break;
          }
        }
      }
      catch(Zend_Gdata_App_Exception $e) {
        $this->assertEqual("ok", $e->getResponse()->getBody());
      }
      $this->assertEqual($entriesFound, 3);
    }


}
